home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap6 / 6_5 / dirsub_p / handler.pl < prev    next >
Encoding:
Perl Script  |  1996-06-15  |  2.2 KB  |  97 lines

  1. #!/usr/bin/perl
  2.  
  3. require "parsehtm.pl";
  4.  
  5. sub directiveHandler
  6. {
  7.     local($tagString,$argString,$endString,%tagDict) 
  8.     = @_;
  9.  
  10.     local($retVal,$type,$endTag,$handler);
  11.  
  12.     $type = $tagDict{"TYPE"};
  13.  
  14.     #We are going to handle directives like tags
  15.     # But the handlers are registered with their type
  16.     # prepended by a directive.
  17.  
  18.     $type = "directive.".$type;
  19.  
  20.     # Look for an end tag. These are registered in the %endTags
  21.     # global associative array.
  22.     
  23.     $endTag = $endTags{$type};
  24.  
  25.     # Look for a handler subroutine for the directive type.
  26.     # These are registered in the %handlerDict global
  27.     # associative array.
  28.     
  29.     $handler = $handlerDict{$type};
  30.     
  31.     # If no handler is found, remove the directive
  32.     
  33.     if(!($handler))
  34.     {
  35.     $retVal = "";
  36.     
  37.     return $retVal;
  38.     }
  39.  
  40.     # If the tag wants the data to the end of the line
  41.     # use mainHtmlParser to read to the end of the line, then
  42.     # call the tag's handler subroutine with the data to the
  43.     # end of the line.
  44.     
  45.     if($endTag eq "eol")    # Tag that needs data to eol
  46.     {
  47.     $argString = &mainHtmlParser("","\n");
  48.  
  49.     $evalString = "&".$handler.'($tagString,$argString,0,%tagDict);';
  50.     }
  51.     elsif($endTag)        # Tag with an end tag
  52.     {
  53.         # Use mainHtmlParser to read any text, up to
  54.     # the end tag. Remove the end tag from the sting.
  55.     # 
  56.     $argString = &mainHtmlParser($endTag,0);
  57.     $argString =~ s/<.*>$//; # Remove the end tag
  58.     
  59.     # Call the tag's handler
  60.     $evalString = "&".$handler.'($tagString,$argString,$endTag,%tagDict);';
  61.     }
  62.     else
  63.     {
  64.         #For unary tags, simply call the handler.
  65.     $evalString = "&".$handler.'($tagString,0,0,%tagDict);';
  66.     }
  67.     
  68.     $retVal = eval($evalString);
  69.  
  70.     return $retVal
  71. }
  72.  
  73. $handlerDict{"DIRECTIVE"} = "directiveHandler";
  74.  
  75. sub toolbarHandler
  76. {
  77.     local($tagString,$argString,$endString,%tagDict) 
  78.     = @_;
  79.     local($retVal);
  80.  
  81.     $retVal = "<INPUT TYPE=\"SUBMIT\" NAME=\"toolbar\" VALUE=\"Previous\">\n";
  82.     $retVal .= "<INPUT TYPE=\"SUBMIT\" NAME=\"toolbar\" VALUE=\"First\">\n";
  83.     $retVal .= "<INPUT TYPE=\"SUBMIT\" NAME=\"toolbar\" VALUE=\"Index\">\n";
  84.     $retVal .= "<INPUT TYPE=\"SUBMIT\" NAME=\"toolbar\" VALUE=\"Next\">\n";
  85.     $retVal .= "<P>";
  86.  
  87.     return $retVal;
  88. }
  89.  
  90. $handlerDict{"directive.toolbar"} = "toolbarHandler";
  91.  
  92. 1;
  93.  
  94.  
  95.  
  96.  
  97.